
#include<bits/stdc++.h>

#ifndef DEBUG
#define NDEBUG
#endif

using namespace std;

inline void update(long long &t, int &k, const long long &used, array<const int,3> arr){    
    
    int map[3]={2,1,3};
    int i=0;
    for(i=0;i<3;i++){
        if(map[i]==k)
            break;
    }
    t -= used;
    t %= arr[0] + arr[1] + arr[2];
    while(t <= 0){
        i = (i+1)%3;
        t += arr[i];            
    }
    k = map[i];
}

int main(){
    int r,y,g;//red, yellow, green
    int n;
    
    cin >> r >> y >> g >> n;
    vector<int> k(n);
    vector<long long>t(n);    
    long long used_time = 0;
    for(int i=0;i<n;i++){
        cin >> k[i] >> t[i];
    }
    for(int i=0;i<n;i++){        
        if(k[i] == 2)
            used_time += t[i] + r;
        else if(k[i] != 3)
            used_time += t[i];        
        
        //更新下一个路口状态
        if(i<=n-2 && k[i+1]!=0)
            update(t[i+1],k[i+1],used_time,{y,r,g});

    }
    cout << used_time;
    return 0;
}
